package com.example.myapplication.fragment

import android.os.Bundle
import android.os.Handler
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.myapplication.R
import com.example.myapplication.adapter.IndexBannerAdapter
import com.example.myapplication.adapter.IndexGridAdapter
import com.example.myapplication.adapter.IndexListAdapter
import com.example.myapplication.adapter.IndexListAdapterTest
import com.example.myapplication.bean.IndexGridBean
import com.example.myapplication.bean.IndexListBean
import com.example.myapplication.databinding.FragmentIndexBinding
import com.example.myapplication.utils.GsonUtils
import com.example.myapplication.utils.NetUtils
import java.util.Timer
import java.util.TimerTask
import kotlin.collections.ArrayList

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [IndexFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class IndexFragment : Fragment() {
    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        // Inflate the layout for this fragment
        val binding = FragmentIndexBinding.inflate(inflater)
        val bannerImages = arrayOf(R.drawable.banner01, R.drawable.banner02, R.drawable.banner03)
        binding.bannerViewPager.adapter = IndexBannerAdapter(bannerImages)
        val timer = Timer()
        timer.schedule(object : TimerTask() {
            override fun run() {
                binding.bannerViewPager.post {
                    if (binding.bannerViewPager.currentItem == bannerImages.size - 1) {
                        binding.bannerViewPager.currentItem = 0
                    } else {
                        binding.bannerViewPager.currentItem++
                    }
                }
            }

        }, 3000, 3000)
//        NetUtils.get("news/findall", null, Handler {
//            val data = GsonUtils.jsonArr(it.obj as String)
//            binding.listRecyclerView.adapter = IndexListAdapterTest(data)
//            binding.listRecyclerView.layoutManager = LinearLayoutManager(binding.root.context)
//            false
//        })
        val gridTitles = arrayOf("场内找车", "车里程", "案例发布", "城市地铁")
        val gridImages =
            arrayOf(R.drawable.grid01, R.drawable.grid02, R.drawable.grid03, R.drawable.grid04)
        val gridList = ArrayList<IndexGridBean>()
        for (i in gridImages.indices) {
            val indexGridBean = IndexGridBean()
            indexGridBean.image = gridImages[i]
            indexGridBean.title = gridTitles[i]
            gridList.add(indexGridBean)
        }
        val listList = ArrayList<IndexListBean>()
        for (i in 0 until 10) {
            val indexListBean = IndexListBean()
            indexListBean.image = R.drawable.grid02
            indexListBean.title = "标题$i"
            indexListBean.content = "内容$i"
            listList.add(indexListBean)
        }
        binding.gridRecyclerView.adapter = IndexGridAdapter(gridList)
        binding.gridRecyclerView.layoutManager = GridLayoutManager(binding.root.context, 4)
        return binding.root
    }

    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment IndexFragment.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            IndexFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
}